import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import { HttpProvider } from '../../providers/http/http';
import { StorageProvider } from '../../providers/storage/storage';
import { DialogProvider } from '../../providers/dialog/dialog';
import { UnitsProvider } from '../../providers/units/units';
/**
 * Generated class for the AssistantofficereturnPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-assistantofficereturn',
  templateUrl: 'assistantofficereturn.html',
})
export class AssistantofficereturnPage {

/**
     * 输入界面控制
     */
    inputSelectView = {
      applyoaofficeretDefitems: new Array(), //办公用品管理(归还)
    }
    /**
     * 输入界面控制
     */
    inputViewModel = {
      applyapplymember: '', //申请人
      applycompany: new Object(), //申请公司
      applydepartment: new Object(), //申请部门
      applychangenote: '', //申请理由
      //
      applynote: '', //备注
      applyuploadfile: '', //附件
      applynownode: '', //当前节点
      applynowmember: '', //当前处理人
      applynextnode: '', //下一步审批节点
      applynextman: '', //下一步审批人
    }
  
    constructor(
      public navCtrl: NavController,
      public navParams: NavParams,
      public http: HttpProvider,
      public storage: StorageProvider,
      public dialog: DialogProvider,
      public units: UnitsProvider) {
    }

  ionViewDidLoad() {
    console.log('ionViewDidLoad AssistantofficereturnPage');
    this.ionicLoad();
  }

  //当前节点
  ionicLoad() {
    //控制显示
    this.getUserInfo();
    this.inputViewModel.applynownode = '发起申请'; //当前节点
    this.inputViewModel.applynextnode = '部门经理审批'; //当前节点
  }
  //获取本用户信息
  getUserInfo() {
    this.storage.getStorage("user").then(val => {
      //请求方式如下
      let params = {
        user_id: val['user_id']
      };
      this.http.postJson('user', 'douserinfo.do', params).then(res => {
        if (res['success'] == true) {
          let info = res['data'];
          //请求成功
          this.inputViewModel.applyapplymember = info['name'];
          this.inputViewModel.applynowmember = info['name'];
        } else {
          this.dialog.showToast("top", res['msg']);
        }
      });
    });
  }
  //初始化选择公司
  applycompanyselect() {
    //共通弹出
    let data = new Object();
    data['selectitem'] = this.inputViewModel.applycompany;
    data['multiselect'] = false; //false 单选 true 多选
    let opts = new Object();
    this.dialog.showModal("ModalcompanyselectPage", data, opts).then(success => {
      //界面关闭的时候
      if (success) {
        let result = success['result'];
        this.inputViewModel.applycompany = result[0];
      }
    });
  }
  //初始化选择部门
  applydepartselect() {
    //共通弹出
    let data = new Object();
    data['selectitem'] = this.inputViewModel.applydepartment;
    data['multiselect'] = false; //false 单选 true 多选
    let opts = new Object();
    this.dialog.showModal("ModaldepartselectPage", data, opts).then(success => {
      //界面关闭的时候
      if (success) {
        let result = success['result'];
        this.inputViewModel.applydepartment = result[0];
      }
    });
  }
  //初始化选择用户
  applyapplymemberselect() {
    //共通弹出
    let data = new Object();
    data['selectitem'] = this.inputViewModel.applynextman;
    data['multiselect'] = false; //false 单选 true 多选
    let opts = new Object();
    this.dialog.showModal("ModaluserselectPage", data, opts).then(success => {
      //界面关闭的时候
      if (success) {
        let result = success['result'];
        this.inputViewModel.applynextman = result[0];
      }
    });
  }
	//初始化选择办公用品
	applyapplyofficeret() {
		//共通弹出
		let data = new Object();
		let opts = new Object();
		this.dialog.showModal("ModalofficeselectPage", data, opts).then(success => {
			//界面关闭的时候
			if (success) {
				let result = success['result'];
				//存在的id
				let ids = new Object();
				for (let i = 0; i < this.inputSelectView.applyoaofficeretDefitems.length; i++) {
					let item = this.inputSelectView.applyoaofficeretDefitems[i];
					ids[item['id']] = item;
				}
				for (let i = 0; i < result.length; i++) {
					let item = result[i];
					if (ids[item['id']] == undefined) {
						item['applynum'] = 1;
						this.inputSelectView.applyoaofficeretDefitems.push(item);
					}
				}
			}
		});
	}
	//选择办公用品的删除
	applyapplyoffiretcedelete(obj: any) {
		for (let i = 0; i < this.inputSelectView.applyoaofficeretDefitems.length; i++) {
			let item = this.inputSelectView.applyoaofficeretDefitems[i];
			if (obj['id'] == item['id']) {
				this.inputSelectView.applyoaofficeretDefitems.splice(i, 1);
				break;
			}
		}
	}
  //提交数据
  applyapplydone() {
    if (this.checkInput()) {
      //初始化
      this.storage.getStorage("user").then(val => {
        //请求方式如下
        let params = {
          user_id: val['user_id'],
        };
        //收文传阅列表
        this.http.postJson('oa', 'leave/insert.do', params).then(res => {
          if (res['data'] != undefined) {

          } else {

          }
        });
      });
    }
  }

  //检查输入
  checkInput() {

    return true;
  }

}
